/*
* Copyright© 2018 Shanxi Lu'an Taihang Laihui Net Tech co.,LTD.
*/
package com.laihuichuxing.carpool.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.laihuichuxing.carpool.dao.CarpoolPassengerStrokeDao;
import com.laihuichuxing.common.util.JsonUtil;
import com.laihuichuxing.config.ResponseCode;
import com.laihuichuxing.order.carpool.api.CarpoolPassengerApi;
import com.laihuichuxing.order.carpool.entity.PassengerStrokeEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @author YangGuang
 * @program: order-server
 * @description:
 * @create: 2018-11-19 17:21
 **/
@Slf4j
@Service(version = "1.0.0",interfaceClass = CarpoolPassengerApi.class)
public class CarpoolPassengerProvider implements CarpoolPassengerApi {

    @Autowired
    private CarpoolPassengerStrokeDao carpoolPassengerStrokeDao;

    @Override
    public Map<String, Object> publish(PassengerStrokeEntity passengerStrokeEntity) {
        try {
            // 判断是否存在进行中行程
            int existStroke = carpoolPassengerStrokeDao.judgeExistStroke(passengerStrokeEntity.getUserId());
            if (existStroke > 0) {
                return JsonUtil.getJson(ResponseCode.STROKE_IS_EXISTING, "行程发布失败,当前存在进行中行程");
            }
            // 保存订单
            int count = carpoolPassengerStrokeDao.insert(passengerStrokeEntity);
            if (count > 0) {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("passengerTradeNo",passengerStrokeEntity.getPassengerStrokeId());
                return JsonUtil.getJson(ResponseCode.SUCCESS, "行程发布成功",resultMap);
            }
            return JsonUtil.getJson(ResponseCode.FAIL, "行程发布失败");
        } catch (Exception e) {
            log.error("顺风车乘客发布行程异常,异常信息为:{}", e.toString(), e);
            return JsonUtil.getJson(ResponseCode.SERVER_ERROR, "服务器异常");
        }
    }
}
